package cn.calm.xhtml.api.service;

import java.io.IOException;
import java.io.Writer;

import cn.calm.xhtml.api.exception.XhtmlException;

public class ResponseWriter {
	private Writer writer;

	public ResponseWriter(Writer writer) {
		this.writer = writer;
	}

	public void startTag(String name) throws XhtmlException {
		try {
			writer.write("<");
			writer.write(name);
		} catch (IOException e) {
			throw new XhtmlException(e);
		}
	}

	public void write(Object value) throws XhtmlException {
		try {
			writer.write(value == null ? "" : value.toString());
		} catch (IOException e) {
			throw new XhtmlException(e);
		}
	}

	public void writeAttr(String name, Object value) throws XhtmlException {
		try {
			writer.write(" ");
			writer.write(name);
			writer.write("=\"");
			writer.write(value == null ? "" : value.toString());
			writer.write("\"");
		} catch (IOException e) {
			throw new XhtmlException(e);
		}
	}

	public void endTag(String name) throws XhtmlException {
		try {
			writer.write("</");
			writer.write(name);
			writer.write(">");
		} catch (IOException e) {
			throw new XhtmlException(e);
		}
	}

	public Writer getWriter() {
		return writer;
	}

	public void setWriter(Writer writer) {
		this.writer = writer;
	}

}
